const Activity = require("../../models/activity");
const { Op } = require("sequelize");

//新增一条数据
exports.addActivity = async function(activity){
    return Activity.create({
        ...activity
    })
}

//获取一条数据
exports.getActivity = async function(id){
    return Activity.findByPk(id,{
        attributes:{exclude:['createdAt','updatedAt','deletedAt']}
    });
}

//更新一条数据
exports.updateActivity = async function(id,activity){
    delete activity.id;
    return Activity.update(
        {...activity},
        {where: {id: id}, individualHooks: true}
    )
}

//删除一条数据
exports.deleteActivity = async function(id){
    return Activity.destroy({
        where:{id},
        individualHooks:true
    })
}

//获取活动列表
exports.getActivityList = async function(keyword,page,size){
    page = Number(page);
    size = Number(size);
    const {rows,count} = await Activity.findAndCountAll({
        where:{
            title:{
                [Op.like]: `%${keyword}%`
            }
        },
        attributes:{exclude:['createdAt','updatedAt','deletedAt']},
        limit:size,
        offset:(page-1) * size,
        order: [['endJoin1', 'ASC']],
    });
    return {
        rows,count
    }
}
